package net.livingflow.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.stereotype.Service;

import net.livingflow.model.SharedData;

/**
 * @author xueqiang.mi
 * @since 2010-12-19
 */
@Service
public class SharedDataService extends BaseService {

	public void add(SharedData sd) {
		Date now = new Date();
		sd.setCrtDate(now);

		save(sd);
	}

	public SharedData get(Long id) {
		return get(SharedData.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<SharedData> lattest() {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(SharedData.class);
			query.setOrdering("crtDate desc");
			query.setRange(0, 30);
			Collection<SharedData> c = pm.detachCopyAll((Collection<SharedData>) query.execute());
			return new ArrayList<SharedData>(c);
		} finally {
			pm.close();
		}
	}
}
